package com.pdstudio.weather;

import com.pdstudio.app.Application;
import com.pdstudio.bean.WeatherFine;
import com.pdstudio.http.WeatherHttpMethod;
import com.pdstudio.util.ImageUtils;
import com.pdstudio.util.SharePreferenceUtil;
import com.pdstudio.view.SwipeBackActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class WeatherVeryClearActivity extends SwipeBackActivity {

	private TextView txtTitle;
	private View customSunTitleView;
	private Handler handler = null;
	private WeatherFine fine;
	private TextView tvTime;// tv_clear_updatetime
	private ImageView ivImg;// iv_image_show
	private SharePreferenceUtil mSpUtil;
	private Application mApplication;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_clear_weather);
		
		//Application.mListeners.add(this);
		mApplication = Application.getInstance();
		mSpUtil = mApplication.getSharePreferenceUtil();
		
//		if (true) {// TODO 这里判断天气情况
//			LinearLayout layout = (LinearLayout) findViewById(R.id.activity_clear_weather);
//			layout.setBackground(getResources().getDrawable(
//					R.drawable.bg0_fine_day));
//		}
		
		LinearLayout layout = (LinearLayout) findViewById(R.id.activity_clear_weather);
		int resId = mApplication.getApplicationBackground(mSpUtil
				.getWeatherStatus());
		layout.setBackground(getResources().getDrawable(resId));


		customSunTitleView = findViewById(R.id.title_weather_detail);
		txtTitle = (TextView) customSunTitleView
				.findViewById(R.id.title2_text_nav);
		txtTitle.setText("精细预报");

		setForecast();

	}

	private void setForecast() {
		tvTime = (TextView) findViewById(R.id.tv_clear_updatetime);
		ivImg = (ImageView) findViewById(R.id.iv_image_show);

		// 开一条子线程加载网络数据
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(0);
					WeatherHttpMethod http = new WeatherHttpMethod(
							WeatherVeryClearActivity.this);
					fine = http.GetWeatherFine(mSpUtil.getAuthCode(),mApplication.getCurSimpleCity().cityCode, "1");
					Message msg = handler.obtainMessage(0, fine);
					handler.sendMessage(msg);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};

		try {
			// 开启线程
			new Thread(runnable).start();
			// handler与线程之间的通信及数据处理
			handler = new Handler() {
				public void handleMessage(Message msg) {
					if (msg.what == 0) {
						// Toast.makeText(getApplicationContext(), "加载数据中",
						// 0).show();
						// msg.obj是获取handler发送信息传来的数据
						if (msg != null && msg.obj != null) {
							fine = (WeatherFine) msg.obj;

							tvTime.setText(fine.getPostTime());

							// ImageUtils.downloadAsyncTask(ivImg,
							// fine.getUri());
							// ivImg.setImageBitmap(bitMap);
							ImageUtils asynImageLoader = new ImageUtils();
							asynImageLoader.showImageAsyn(ivImg, fine.getUri(),
									R.drawable.bgsun);
						}
					}
				}
			};
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
